package com.example.myapplication2;

import android.app.TimePickerDialog;
import android.view.View;
import android.widget.TextView;
import android.widget.TimePicker;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

import java.util.Calendar;

public class TimePickerDialogActivity extends AppCompatActivity implements View.OnClickListener, TimePickerDialog.OnTimeSetListener {
    TextView textView;
    TimePicker timePicker;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_time_picker_dialog);
        textView = findViewById(R.id.TimePickerDialogText);
        timePicker = findViewById(R.id.TimePicker);
        //设置为24小时模式
        timePicker.setIs24HourView(true);
        findViewById(R.id.TimePickerButton).setOnClickListener(this);
        findViewById(R.id.TimePickerButton2).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.TimePickerButton:
                textView.setText("选中时间为"+timePicker.getHour()+":"+timePicker.getMinute());
                break;
            case R.id.TimePickerButton2:
                //默认显示
                Calendar calendar=Calendar.getInstance();
                //其中的android.R.style.Theme_Holo_Dialog,为弹窗风格
                TimePickerDialog datePickerDialog = new TimePickerDialog(this, android.R.style.Theme_Dialog,this,
                        calendar.get(Calendar.HOUR),calendar.get(Calendar.MINUTE),true);

                datePickerDialog.show();
                break;
        }
    }

    @Override
    public void onTimeSet(TimePicker timePicker, int i, int i1) {
        textView.setText("弹窗选中时间为"+timePicker.getHour()+":"+timePicker.getMinute());
    }
}